package txt2vz;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.tika.Tika;


@SuppressWarnings("serial")
public class URLServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {

		String strCallResult = "";
		resp.setContentType("text/plain");
		try {
			// Extract out the user entered ISBN number
			String strURL = req.getParameter("u");
			
			System.out.println("in urlservet u: " +strURL);

		// Do basic validation
			if (strURL == null)
				throw new Exception("URL field cannot be null");

			// Trim
			strURL = strURL.trim();
			if (strURL.length() == 0)
				throw new Exception("URL field cannot be empty");
	

			URL url = new URL(strURL);		
			

			Tika t = new Tika();
			
	    	String	s = t.parseToString(url);		
			
//			BufferedReader reader = new BufferedReader(
//					new InputStreamReader(					
//					url.openStream()));
//			StringBuffer response = new StringBuffer();
//			String line;
//
//			while ((line = reader.readLine()) != null) {
//				response.append(line);
//			}
//			reader.close();
//
//			strCallResult = response.toString();
//
//			resp.getWriter().println(strCallResult);
	    	resp.getWriter().println(s);

		} catch (Exception ex) {
			strCallResult = "Error: " + ex.getMessage();
			resp.getWriter().println(strCallResult);
		}
	}

	@Override
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doGet(req, resp);
	}
}

